package tacos;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import tacos.domain.Ingredient;
import tacos.repository.IngredientRepository;
import tacos.domain.Ingredient.Type;
import tacos.simpleflow.FileWriterGateway;

@SpringBootApplication
public class TacoCloudApplication {

    public static void main(String[] args) {
        System.out.println("Spring DevTools Test!!!");
        SpringApplication.run(TacoCloudApplication.class, args);
    }

    @Bean
    public CommandLineRunner dataLoader(IngredientRepository repo) {
        return new CommandLineRunner() {
            @Override
            public void run(String... args) throws Exception {
                repo.save(new Ingredient("FLTO", "Flour Tortilla", Type.WRAP));
                repo.save(new Ingredient("COTO", "Corn Tortilla", Type.WRAP));
                repo.save(new Ingredient("GRBF", "Ground Beef", Type.PROTEIN));
                repo.save(new Ingredient("CARN", "Carnitas", Type.PROTEIN));
                repo.save(new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES));
                repo.save(new Ingredient("LETC", "Lettuce", Type.VEGGIES));
                repo.save(new Ingredient("CHED", "Cheddar", Type.CHEESE));
                repo.save(new Ingredient("JACK", "Monterrey Jack", Type.CHEESE));
                repo.save(new Ingredient("SLSA", "Salsa", Type.SAUCE));
                repo.save(new Ingredient("SRCR", "Sour Cream", Type.SAUCE));
            }
        };
    }

    @Bean
    public CommandLineRunner writeData(FileWriterGateway gateway, Environment env) {
        /**
         * 测试Spring Integration file
         */
        return args -> {
            String[] activeProfiles = env.getActiveProfiles();
            if (activeProfiles.length > 0) {
                String profile = activeProfiles[0];
                gateway.writeToFile("simple.txt", "Hello, Spring Integration! (" + profile + ")");
            } else {
                System.out.println("No active profile set. Should set active profile to one of xmlconfig, javaconfig, or javadsl.");
            }
        };
    }

}
